package com.hhkj.study.admin.resource.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.resource.entity.ResourceQuestionBank;
import com.hhkj.study.admin.resource.excel.ResourceQuestionBankExcelRes;
import com.hhkj.study.admin.resource.model.podo.ResourceQuestionBankDo;
import com.hhkj.study.admin.resource.model.podo.ResourceQuestionBankPo;
import com.hhkj.study.admin.resource.model.req.ResourceQuestionBankReq;
import com.hhkj.study.admin.resource.model.req.ResourceQuestionBatchSave;
import com.hhkj.study.admin.resource.model.res.ResourceQuestionBankRes;
import com.hhkj.study.admin.resource.service.base.ResourceQuestionBankService;
import com.hhkj.study.admin.resource.service.biz.ResourceQuestionBankBizService;
import com.hhkj.study.admin.resource.service.extend.ResourceQuestionBankExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.model.res.ExcelImportResultRes;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* ResourceQuestionBankBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ResourceQuestionBankBizServiceImpl extends BaseBizServiceImpl<ResourceQuestionBank, ResourceQuestionBankReq, ResourceQuestionBankRes, ResourceQuestionBankDo, ResourceQuestionBankPo, ResourceQuestionBankService, ResourceQuestionBankExtendService> implements ResourceQuestionBankBizService {

    @Resource
    private GlobalCache globalCache;

    @Override
    protected void afterPage(List<ResourceQuestionBankRes> questionBankResList) {
        supplyQuestionBank(questionBankResList);
    }

    @Override
    protected void beforeSave(ResourceQuestionBankReq resourceQuestionBankReq) {
        ResourceQuestionBank questionBank = this.getExtendService().getByNameCourseType(resourceQuestionBankReq.getName(), resourceQuestionBankReq.getCourseType());
        if (ObjectUtil.isNotNull(questionBank)) {
            throw new GlobalException(resourceQuestionBankReq.getName() + "题库名称已存在");
        }
    }

    @Override
    protected void beforeUpdate(ResourceQuestionBankReq resourceQuestionBankReq) {
        ResourceQuestionBank questionBank = this.getExtendService().getByIdNotNull(resourceQuestionBankReq.getId());
        if (!StrUtil.equals(questionBank.getName(), resourceQuestionBankReq.getName())) {
            ResourceQuestionBank questionBankByName = this.getExtendService().getByNameCourseType(resourceQuestionBankReq.getName(), resourceQuestionBankReq.getCourseType());
            if (ObjectUtil.isNotNull(questionBankByName)) {
                throw new GlobalException(resourceQuestionBankReq.getName() + "题库名称已存在");
            }
        }
    }

    @Override
    protected void afterGetById(ResourceQuestionBankRes resourceQuestionBankRes) {
        supplyQuestionBank(Lists.newArrayList(resourceQuestionBankRes));
    }

    /**
     * 补充题库其他字段
     * @param questionBankResList
     */
    private void supplyQuestionBank(List<ResourceQuestionBankRes> questionBankResList) {
        Map<Long, String> dictMap = globalCache.cacheDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode(), DictCodeEnum.BANK_TYPE.getCode()));
        questionBankResList.forEach(res -> {
            res.setTypeName(dictMap.get(res.getType()));
            res.setCourseTypeName(dictMap.get(res.getCourseType()));
        });
    }

    @Override
    public boolean updatePublishFlag(ResourceQuestionBankReq req) {
        ResourceQuestionBank questionBank = this.getExtendService().getByIdNotNull(req.getId());
        if (questionBank.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<ResourceQuestionBank> bankUpdate = new LambdaUpdateWrapper<>();
        bankUpdate.eq(ResourceQuestionBank::getId, req.getId())
                .set(ResourceQuestionBank::getPublishedFlag, req.getPublishedFlag());
        return this.getService().update(bankUpdate);
    }

    @Override
    public void export(ResourceQuestionBankReq req, HttpServletResponse response) {
        List<ResourceQuestionBankRes> questionBankResList = list(req);
        if (CollectionUtil.isEmpty(questionBankResList)) {
            throw new GlobalException("没有数据");
        }
        ExcelUtils.exportExcelRuoYi(response, BeanUtil.copyToList(questionBankResList, ResourceQuestionBankExcelRes.class), ResourceQuestionBankExcelRes.class, "题库信息", "题库信息");
    }

    @Override
    public ExcelImportResultRes importQuestionBank(MultipartFile file, HttpServletRequest request) {
        return null;
    }

    @Override
    public boolean batchSave(ResourceQuestionBatchSave questionBatchSave) {
        List<ResourceQuestionBank> resourceQuestionBankList = questionBatchSave.getAssocOutBankList().stream().map(outBank -> {
            ResourceQuestionBank resourceQuestionBank = new ResourceQuestionBank();
            resourceQuestionBank.setAssoBankId(outBank.getAssoBankId());
            resourceQuestionBank.setAssoBankName(outBank.getAssoBankName());
            resourceQuestionBank.setName(outBank.getAssoBankName());
            resourceQuestionBank.setQuestionCount(outBank.getQuestionCount());
            resourceQuestionBank.setAssoCourseId(outBank.getAssoCourseId());
            resourceQuestionBank.setYear(questionBatchSave.getYear());
            resourceQuestionBank.setCourseType(questionBatchSave.getCourseType());
            resourceQuestionBank.setType(questionBatchSave.getType());
            resourceQuestionBank.setPublishedFlag(questionBatchSave.getPublishedFlag());
            resourceQuestionBank.setRemark(questionBatchSave.getRemark());
            return resourceQuestionBank;
        }).collect(Collectors.toList());
        return this.getService().saveBatch(resourceQuestionBankList);
    }
}